package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.vo.ChapterVo;
import com.atguigu.eduservice.entity.vo.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程章节 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-04-07
 */
@Service
@CrossOrigin
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {


    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public ArrayList getChapterVideo(String courseId) {
        ArrayList all = new ArrayList();
//        根据课程id查询章节数据.
        LambdaQueryWrapper<EduChapter> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduChapter::getCourseId, courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(wrapper);

//       课程id 查询所有小节.
        LambdaQueryWrapper<EduVideo> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(EduVideo::getCourseId, courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(wrapper1);


//       根据课程id找出对应章节的数据.
//        一个chatperVo是一个章节.children是这个章节的小结.
        for (int i = 0; i < eduChapters.size(); i++) {
            ChapterVo chapterVo = new ChapterVo();
            EduChapter eduChapter = eduChapters.get(i);
            BeanUtils.copyProperties(eduChapter, chapterVo);

            List eduVideo = new ArrayList();
            for (int j = 0; j < eduVideoList.size(); j++) {
                VideoVo videoVo = new VideoVo();
                if (eduChapter.getId().equals(eduVideoList.get(j).getChapterId())) {
                    BeanUtils.copyProperties(eduVideoList.get(j), videoVo);
                    eduVideo.add(videoVo);
                }
            }
            chapterVo.getChildren().addAll(eduVideo);
            all.add(chapterVo);
        }

//        根据章节数据找到对应video数据.
        return all;
    }

    /**
     * 判断有无数据
     *
     * @param courseId
     * @return
     */
    @Override
    public boolean isExitisChild(String courseId) {
//        父亲节点在一张表上,子节点在另一张表,依靠父id与子parent_id关联.
        LambdaQueryWrapper<EduVideo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduVideo::getCourseId, courseId);
        int count = eduVideoService.count(wrapper);
        if (count > 0) {
            throw new GuliException(20001, "该章节下有小结");
        } else {
            int i = baseMapper.deleteById(courseId);
            return i > 0;
        }
    }
}
